package com.rzjm.api;

import com.rzjm.api.netty.NettyServer;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;

@SpringBootApplication
@Transactional
@EnableScheduling  //  执行 定时任务
@MapperScan({"com.rzjm.api.dao"})
//@ComponentScan(basePackages = {"com.rzjm.api.*"})
@EnableTransactionManagement
@ServletComponentScan
public class CheckApiApplication {


    @Value("${web.isRsServer}")
    private int isRsServer;

    /*添加代码 begin*/
    private static CheckApiApplication checkApiApplication;


    @PostConstruct
    public void init(){
        checkApiApplication = this;
        checkApiApplication.isRsServer = this.isRsServer;
    }


    public static void main(String[] args) {
        //SpringApplication.run(GovernApiApplication.class, args);
        //启动WEB项目
        try{
            SpringApplication application = new SpringApplication(CheckApiApplication.class);
            ConfigurableApplicationContext context = application.run(args);

            if(checkApiApplication.isRsServer == 1){
                //线程外面的方法
                new Thread(new Runnable() {
                    public void run() {
                        //线程里面的内容
                        //启动服务端
                        CGQRSServer c = context.getBean(CGQRSServer.class);
                        try {
                            c.start();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            } else if(checkApiApplication.isRsServer == 0) {
                //线程外面的方法
                new Thread(new Runnable() {
                    public void run() {
                        //线程里面的内容
                        //启动服务端
                        NettyServer nettyServer = context.getBean(NettyServer.class);
                        nettyServer.start();
                    }
                }).start();
            }


        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
